package lt.liepasoft.sudoku;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lt.liepasoft.sudoku.component.Data;
import lt.liepasoft.sudoku.component.PaintBackground;
import lt.liepasoft.sudoku.component.PaintComponent;
import lt.liepasoft.sudoku.component.PaintGrid;
import lt.liepasoft.sudoku.component.PaintNumbers;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;

public class SudokuView extends View implements OnGestureListener {
	
	private GestureDetector gestureScanner;
	
	private Map <Data, Integer> options;
	private List <PaintComponent> components;
	
	private SudokuTable mainSudokuTable;
	private SudokuSolver sudokuSolver;
	private SudokuPuzzleMaker sudokuMaker;
	
	private PaintNumbers numbersPaint;
	
	public SudokuView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}


    public SudokuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }		

	private void init() {
		options = new HashMap<Data, Integer>();
		options.put(Data.SCREEN_WIDTH, SudokuActivity.gameWidth);
		options.put(Data.SCREEN_HEIGHT, SudokuActivity.gameHeight);
		options.put(Data.GRID_OFFSET_X, 30);
		options.put(Data.GRID_OFFSET_Y, 30);
		options.put(Data.CELL_HEIGHT, 45);
		options.put(Data.CELL_WIDTH, 45);
		options.put(Data.TEXT_SIZE, 30);
		options.put(Data.LINE_STROKE_WIDTH, 5);
		
		sudokuSolver = new SudokuSolver();
		sudokuMaker = new SudokuPuzzleMaker();
		mainSudokuTable = sudokuMaker.createPuzzle();		
		
		components = new ArrayList<PaintComponent>();
		components.add(new PaintBackground(options));
		components.add(new PaintGrid(options));
		numbersPaint = new PaintNumbers(options, mainSudokuTable);
		components.add(numbersPaint);
		
		this.gestureScanner = new GestureDetector(this); 
        this.setOnTouchListener(new OnTouchListener() {			
			public boolean onTouch(View v, MotionEvent event) {
				return gestureScanner.onTouchEvent(event);
			}
		});
	}

	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
    protected void onDraw(Canvas canvas) { 
		super.onDraw(canvas);
		for (PaintComponent c: components)
			c.paint(canvas);
	}


	public void newGame() {
		mainSudokuTable = sudokuMaker.createPuzzle();
		numbersPaint.setTable(mainSudokuTable);
		this.invalidate();		
	}	
	
	public void solveGame(){
		sudokuSolver.solveSudoku(mainSudokuTable);
		numbersPaint.setTable(mainSudokuTable);
		this.invalidate();
	}
}
